#include "RenderPch.h"

#include "ShaderDiffuse.h"

ShaderDiffuse::ShaderDiffuse(void) :
ShaderBase()
{
}

ShaderDiffuse::~ShaderDiffuse(void)
{
}

int ShaderDiffuse::Begin( Material * material, DWORD fvf )
{
	m_material = material;
	m_fvf = fvf;

	return 1;
}

void ShaderDiffuse::BeginPass( int pass )
{
	if( pass == 0 )
	{
		RenderFacade::GetInstance()->SetFVF( m_fvf );

		Texture * texture = m_material->GetTextureData("diffuse_texture");

		if( texture )
		{
			RenderFacade::GetInstance()->SetTexture( 0, (LPDIRECT3DTEXTURE9)texture->Get() );
		}
		else
		{
			RenderFacade::GetInstance()->SetTexture( 0, NULL );
		}
		
		RenderFacade::GetInstance()->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_SELECTARG1 );
		RenderFacade::GetInstance()->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
		RenderFacade::GetInstance()->SetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1 );
		RenderFacade::GetInstance()->SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE );

		RenderFacade::GetInstance()->SetRenderState( D3DRS_ALPHABLENDENABLE, TRUE );
		RenderFacade::GetInstance()->SetRenderState( D3DRS_SRCBLEND, D3DBLEND_SRCALPHA );
		RenderFacade::GetInstance()->SetRenderState( D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA );
	}
}

void ShaderDiffuse::EndPass()
{
	RenderFacade::GetInstance()->SetRenderState( D3DRS_ALPHABLENDENABLE, FALSE );
}

void ShaderDiffuse::End()
{
}